#include "targettask.h"



TargetTask::TargetTask(QObject *parent)
{

}

TargetTask::~TargetTask()
{

}

void TargetTask::OnlyAdd(quint8 ID, quint8 targetState)
{
  QWriteLocker writeLocker(&StateOfTargetMapRwLock);
  if (!StateOfTargetMap.contains(ID)) {
      StateOfTargetMap.insert(ID,targetState);
    }
}

void TargetTask::ToUnconnected(int ID)
{
  QWriteLocker writeLocker(&StateOfTargetMapRwLock);
  StateOfTargetMap.remove(ID);
}

void TargetTask::Update(int ID, int targetState)
{
  QWriteLocker writeLocker(&StateOfTargetMapRwLock);
  StateOfTargetMap[ID] = targetState;
}

void TargetTask::DeleteVoltage(int ID)
{
  QWriteLocker writeLocker(&VoltageOfTargetRWLock);
  VoltageOfTargetMap.remove(ID);
}

void TargetTask::UpdateVoltage(int ID, double v)
{
  QWriteLocker writeLocker(&VoltageOfTargetRWLock);
  VoltageOfTargetMap[ID] = v;
}

void TargetTask::DeleteTimer(int ID)
{
  QWriteLocker writeLocker(&TimeMapRWLock);
  TimeMap.remove(ID);
}

void TargetTask::UpdateSoundSpeed(double temperature)
{
  SoundSpeed = 331 * std::sqrt(1.0 + temperature / 273.0);
  qDebug() << QString("当前温度:%1  当前结算的声速 %2").arg( temperature).arg(SoundSpeed);
  if(!(SoundSpeed >=  300 && SoundSpeed <= 380)){
      qDebug() << QString("当前结算的声速 %1,大小不对,已重置为345").arg(SoundSpeed);
      SoundSpeed = 345.0;
    }
}

// CreateE0
//
//	@Desc: 脱靶幅度过大过滤回发
//	@param id
//	@return string
QString TargetTask::CreateE0(int id)
{
  // E0 == 224
  return creatSpecialCommand(id,224);
}

// CreateF0
//
//	@Desc: 脱靶回发
//	@param id
//	@return string
QString TargetTask::CreateF0(int id)
{
  // F0 == 240
  return creatSpecialCommand(id,240);
}

// Create00
//
//	@Desc: 校准回发
//	@param id
//	@return string
QString TargetTask::Create00(int id)
{
  // 00 == 0
  return creatSpecialCommand(id, 0);
}

// CreateFE
//
//		Deprecated
//	 @Desc:
//	 @param id
//	 @return string
QString TargetTask::CreateFE(int id)
{
  // FE == 254
  return creatSpecialCommand(id, 254);
}

// creatSpecialCommand
//
//	@Desc: 抽象出来的数据生成方法
//	@param id
//	@param command
//	@return string
QString TargetTask::creatSpecialCommand(int id, quint16 command)
{
  QString sequenceString = QString::number(RecordSequenceMap[id],16).rightJustified(2, '0');

  QString idString = QString::number(id | 0x80,16).rightJustified(2, '0');

  quint16 idInt = idString.toUInt();

  QString commandString = QString::number(command,16).rightJustified(2, '0');

  quint16 systemId = SystemID;

  QString systemIDString = QString::number(systemId,16).rightJustified(4, '0');

  QVector<quint16> msg0 = {static_cast<quint16>(RecordSequenceMap[id]),
                           static_cast<quint16>(systemId >> 8),
                           static_cast<quint16>(systemId & 0xFF),
                           command,
                           0,
                           0,
                           0,
                           0 };
  quint16 crc = CheckUtils::CheckSumOne(msg0);
  QString crcString = QString::number(crc,16).rightJustified(4, '0');

  QString result = PrefixForWriteBack + idString + LengthOfWriteBackForTargetData +sequenceString
                   +systemIDString + commandString + "00000000" + crcString;

  qDebug()<<"result158:  "<<result;

  return result;
}

// CommonTargetDataMsg
//
//	@Desc: 正常打靶回发的数据
//	"5A" + 地址 + 长度"0A" + 序列号 + (环数+方向) + x高 + x低 + y高 + y低 + CRC
//	@param ID
//	@param sequence
//	@param cnAndDir
//	@param xh
//	@param xl
//	@param yh
//	@param yl
//	@return string
QString TargetTask::CommonTargetDataMsg(int ID, int sequence, int cnAndDir, int xh, int xl, int yh, int yl)
{
  QString idString = QString("%1").arg(ID | 0x80, 2, 16, QChar('0')).toUpper();
  QString sequenceString = QString("%1").arg(sequence, 2, 16, QChar('0')).toUpper();
  QString cnAndDirString = QString("%1").arg(cnAndDir, 2, 16, QChar('0')).toUpper();
  QString xhString = QString("%1").arg(xh, 2, 16, QChar('0')).toUpper();
  QString xlString = QString("%1").arg(xl, 2, 16, QChar('0')).toUpper();
  QString yhString = QString("%1").arg(yh, 2, 16, QChar('0')).toUpper();
  QString ylString = QString("%1").arg(yl, 2, 16, QChar('0')).toUpper();
  quint16  systemId = SystemID;
  QString systemIDString = QString("%1").arg(systemId, 4, 16, QChar('0')).toUpper();
  QVector<quint16> data = { static_cast<quint16>(sequence), static_cast<quint16>(systemId >> 8),
                            static_cast<quint16>(systemId & 0xFF), static_cast<quint16>(cnAndDir),
                            static_cast<quint16>(xh), static_cast<quint16>(xl),
                            static_cast<quint16>(yh), static_cast<quint16>(yl) };
  uint16_t crc = CheckUtils::CheckSumOne(data);
  QString crcString = QString::number(crc, 16).rightJustified(4, '0');

  QString result = PrefixForWriteBack + idString + LengthOfWriteBackForTargetData +
      sequenceString + systemIDString + cnAndDirString +
      xhString + xlString + yhString + ylString + crcString;

  return result;
}


long long int TargetTask::GetUserIdByAddr(int addr){
  QReadLocker readLocker(&TargetBelongToUserRWLock);
  return TargetBelongTOUserMap[addr];
}

void TargetTask::Binding(int addr, long long userID)
{
  QWriteLocker writeLocker(&TargetBelongToUserRWLock);
  TargetBelongTOUserMap[addr] = userID;
}

void TargetTask::UntieAll()
{
  QWriteLocker writeLocker(&TargetBelongToUserRWLock);
  foreach(int key, TargetBelongTOUserMap.keys())
    {
      TargetBelongTOUserMap.remove(key);
    }
}



